package com.xckj.bigdata.demo.controller;

import com.xckj.bigdata.demo.dao.UserDao;
import com.xckj.bigdata.demo.entity.TestRedisEntity;
import com.xckj.bigdata.demo.entity.User;
import com.xckj.bigdata.demo.entity.UserSession;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by 刘嘉民 on 2020/6/2 14:01
 */
@Controller
public class LoginController {

//    @Autowired
//    private AmqpTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate  redisTemplate;
    @Resource
    private UserDao userDao;
    @RequestMapping("/index")
    public String index(Model model){
//        redisTemplate.opsForValue().set("test","815616383");
//        TestRedisEntity redisEntity =  (TestRedisEntity) redisTemplate.opsForValue().get("redisEntity");
//        TestRedisEntity redisEntity = new TestRedisEntity();
//        redisEntity.setId(1);
//        redisEntity.setName("张三");
//        redisEntity.setAge(18);
//        redisEntity.setHobby("basketball");
//        redisEntity.setTel("13576137852");
//        redisTemplate.opsForValue().set("redisEntity",redisEntity,60*60*30, TimeUnit.SECONDS);
//        redisTemplate.
        System.out.println("我进来了");
        model.addAttribute("value","这就是真理吗？");
        return "login";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("userSession");
        return "login";

    }

    @RequestMapping("/register")
    public String register(HttpSession session){
        return "register";
    }

    @RequestMapping("/register.save")
    @ResponseBody
    public String saveRegister(User user){
        return "register";
    }
    @RequestMapping("/rabbitsend")
    public String rabbit(Model model){
//        rabbitTemplate.convertAndSend("topicExchange","topic.ce55","哈哈哈");
        return "login";
    }
     @RequestMapping("/getUsers")
    public Map<String,Object> getUsers(Model model){
         Map<String,Object> map = new HashMap<String,Object>();
         List<User> list= userDao.selectAllUser();
        return map;
    }
    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Object> login(Model model, User user, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
         User user1= userDao.selectByParam(user);
         if (user1!=null){
             UserSession userSession = new UserSession();
             userSession.setEmail(user1.getEmail());
             userSession.setLoginName(user1.getLoginName());
             userSession.setTel(user1.getTel());
             userSession.setUserId(user1.getId());
             userSession.setUserName(user1.getName());
             session.setAttribute("userSession",userSession);
             redisTemplate.opsForValue().set("userSession"+user1.getId(),userSession,60*60*30, TimeUnit.SECONDS);

             map.put("code",1);
         }else{
             map.put("code",0);
             map.put("msg","用户名或密码错误");
         }
        return map;
    }
    @RequestMapping("/mainIndex")
    public String mainIndex(Model model){
        return "index";
    }
}
